import { createRouter, createWebHashHistory } from 'vue-router'
import { useUserInfoStore } from '@/stores'
const routes = [
  {
    path: '/',
    redirect: '/login', // 重定向到你想要的默认路由
  },
  {
    path: '/machine',
    name: 'machine',
    meta: {
      keepAlive: true, //需要缓存
    },
    component: () => import('@/views/machine/index.vue'),
  },
  {
    path: '/alarm',
    name: 'alarm',
    meta: {
      keepAlive: true,
    },
    component: () => import('@/views/alarm/index.vue'),
  },
  {
    path: '/machineData',
    name: 'machineData',
    meta: {
      keepAlive: true,
    },
    component: () => import('@/views/machineData/index.vue'),
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/login.vue'),
  },
  {
    path: '/change_paw',
    name: 'change_paw',
    component: () => import('@/views/login/change_paw.vue'),
  },
  {
    path: '/user_centre',
    name: 'user_centre',
    component: () => import('@/views/user_centre/user_centre.vue'),
  },
  {
    path: '/record',
    name: 'record',
    component: () => import('@/views/alarm/record.vue'),
  },
  {
    path: '/device_details',
    name: 'device_details',
    component: () => import('@/views/machine/device_details.vue'),
  },
  {
    path: '/order_details',
    name: 'order_details',
    component: () => import('@/views/order/order_details.vue'),
  },
  {
    path: '/replenishment',
    name: 'replenishment',
    component: () => import('@/views/replenishment/replenishment.vue'),
  },
  {
    path: '/replenishment-details',
    name: 'replenishment-details',
    component: () => import('@/views/replenishment/replenishment-details.vue'),
  },
  {
    path: '/privacy',
    name: 'privacy',
    component: () => import('@/views/user_agreement/privacy.vue'),
  },
  {
    path: '/agreement',
    name: 'agreement',
    component: () => import('@/views/user_agreement/agreement.vue'),
  },
  //  投资人路由
  {
    path: '/ivestorMachine',
    name: 'ivestorMachine',
    component: () => import('@/views/ivestorMachine/ivestorMachine.vue'),
  },
  {
    path: '/ivestorTradingData',
    name: 'ivestorTradingData',
    component: () => import('@/views/ivestorTradingData/ivestorTradingData.vue'),
  },
  {
    path: '/ivestorSalesDetail',
    name: 'ivestorSalesDetail',
    component: () => import('@/views/ivestorTradingData/ivestorSalesDetail.vue'),
  },
  {
    path: '/ivestorMy',
    name: 'ivestorMy',
    component: () => import('@/views/ivestorMy/ivestorMy.vue'),
  },
  {
    path: '/ivestorChangePaw',
    name: 'ivestorChangePaw',
    component: () => import('@/views/ivestorMy/ivestorChangePaw.vue'),
  },
  {
    path: '/ivestorCash',
    name: 'ivestorCash',
    component: () => import('@/views/ivestorCash/ivestorCash.vue'),
  },
  {
    path: '/ivestorCashTwoPage',
    name: 'ivestorCashTwoPage',
    component: () => import('@/views/ivestorCash/ivestorCashTwoPage.vue'),
  },
  {
    path: '/cashRule',
    name: 'cashRule',
    component: () => import('@/views/ivestorCash/ivestorCashRule.vue'),
  },
  {
    path: '/historyCash',
    name: 'historyCash',
    component: () => import('@/views/ivestorCash/historyCash.vue'),
  },
  {
    path: '/ledgerDetails',
    name: 'ledgerDetails',
    component: () => import('@/views/ivestorCash/ledgerDetails.vue'),
  },
  {
    path: '/cashApplyPage',
    name: 'cashApplyPage',
    component: () => import('@/views/ivestorCash/cashApplyPage.vue'),
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'notFound',
    component: () => import('@/views/login/login.vue'),
  },
]
const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

router.beforeEach((to, from, next) => {
  const userInfoStore = useUserInfoStore()
  const token = userInfoStore?.token

  if (token) {
    if (userInfoStore?.userInfo?.userType === 'investor' && to.path === '/login') {
      next('/ivestorMachine')
    } else if (userInfoStore?.userInfo?.userType === 'customerService' && to.path === '/login') {
      next('/machine')
    } else {
      next()
    }
  } else {
    if (to.path !== '/login' && to.path !== '/change_paw') {
      next('/login')
    } else {
      next()
    }
  }
})
export default router
